package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.customer.mapper.AccountMapper;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.mapper.ServeProviderMapper;
import com.jzo2o.customer.model.domain.Account;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.ServeProvider;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.model.dto.response.BankAccountResDTO;
import com.jzo2o.customer.service.AccountService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description AccountServiceImpl
 * @Author dmx
 * @Date 2024-12-02
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    /**
     * 新增银行账户
     *
     * @param dto
     */


    @Override
    public void addWorkerBankAccount(BankAccountUpsertReqDTO dto) {
        //1.判断dto是否为空，为空的话返回错误结果
        if (BeanUtils.isEmpty(dto)) {
            throw new BadRequestException("账户信息为空");
        }
        //3.将dto的数据拷贝到account实体类中，并插入到表中
        Account account = BeanUtils.copyBean(dto, Account.class);
        account.setServeProviderId(UserContext.currentUserId());
        boolean save = this.saveOrUpdate(account);
        //4.判断是否插入成功
        if (!save) {
            throw new DBException("新增账户失败");
        }

    }


    /**
     * 获取当前用户的银行账号
     * @return
     */
    @Override
    public BankAccountResDTO getAccountByServeProviderId() {
        LambdaQueryWrapper<Account> wrapper = Wrappers.<Account>lambdaQuery()
                .eq(Account::getServeProviderId, UserContext.currentUserId());
        Account account = this.getOne(wrapper);
        if (BeanUtils.isEmpty(account)){
            throw new BadRequestException("用户没有账户");
        }
        BankAccountResDTO bankAccountResDTO = BeanUtils.copyBean(account, BankAccountResDTO.class);
        return bankAccountResDTO;
    }

    /**
     * 机构端--新增或更新银行账户信息
     * @param dto
     * @return
     */
    @Override
    public BankAccountResDTO saveAgencyBankAccount(BankAccountUpsertReqDTO dto) {
        //1.判断dto是否为空，为空的话返回错误结果
        if (BeanUtils.isEmpty(dto)) {
            throw new BadRequestException("账户信息为空");
        }
        //3.将dto的数据拷贝到account实体类中，并插入到表中
        Account account = BeanUtils.copyBean(dto, Account.class);
        account.setServeProviderId(UserContext.currentUserId());
        boolean save = this.saveOrUpdate(account);
        //4.判断是否插入成功
        if (!save) {
            throw new DBException("新增账户失败");
        }
        BankAccountResDTO bankAccountResDTO = BeanUtils.copyBean(account, BankAccountResDTO.class);
        return bankAccountResDTO;
    }
}
